package com.jiang.sign.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiang.sign.Dao.LoginUser;
import com.jiang.sign.Dao.SignUser;
import com.jiang.sign.Mapper.SignUserMapper;
import io.netty.util.internal.StringUtil;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

//自定义校验需要实现这一步
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Resource
    private SignUserMapper signUserMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       //todo 查询用户信息
        QueryWrapper<SignUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        SignUser signUser = signUserMapper.selectOne(queryWrapper);
        //如果没有查询到用户
       if(Objects.isNull(signUser)){
           throw new RuntimeException("用户名或者密码错误");
       }

        //todo 查询对应的权限信息
        //同过sign查询权限信息 对应关系为： 主要是QueryWrapper的连调,这里偷个懒就不写了
        List<String> list = new ArrayList<>(Arrays.asList("admin","system:dept:list"));


        //todo 数据封装为 UserDetails 返回
        return new LoginUser(signUser,list);
    }

}
